//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Base64;
import com.baidu.trace.api.bos.BosGeneratePresignedUrlRequest;
import com.baidu.trace.api.bos.BosGeneratePresignedUrlResponse;
import com.baidu.trace.api.bos.BosGetObjectResponse;
import com.baidu.trace.api.bos.BosObjectRequest;
import com.baidu.trace.api.bos.BosObjectResponse;
import com.baidu.trace.api.bos.BosObjectType;
import com.baidu.trace.api.bos.BosPutObjectRequest;
import com.baidu.trace.api.bos.BosPutObjectResponse;
import com.baidu.trace.api.bos.ImageProcessCommand;
import com.baidu.trace.api.bos.ImageWatermarkCommand;
import com.baidu.trace.api.bos.OnBosListener;
import com.baidu.trace.api.bos.TextWatermarkCommand;
import com.baidu.trace.c.e;
import com.baidu.trace.model.BaseRequest;
import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceSessionCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.BosObjectInputStream;
import com.baidubce.services.bos.model.BosObject;
import com.baidubce.services.bos.model.ObjectMetadata;
import com.baidubce.services.bos.model.PutObjectResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONException;
import org.json.JSONObject;

public final class h {
    private static BosClient a = null;
    private static boolean b = false;

    public h() {
    }

    protected static void a() {
        BosClientConfiguration var0;
        (var0 = new BosClientConfiguration()).setCredentials(new DefaultBceSessionCredentials(g.a, g.c, g.d));
        var0.setEndpoint("gz.bcebos.com");
        var0.setProtocol(Protocol.HTTPS);
        a = new BosClient(var0);
        b = true;
    }

    protected static String a(BosGeneratePresignedUrlRequest var0) {
        StringBuffer var1 = new StringBuffer();
        ImageProcessCommand var2 = var0.getImageProcessCommand();
        ImageWatermarkCommand var3 = var0.getImageWatermarkCommand();
        TextWatermarkCommand var4 = var0.getTextWatermarkCommand();
        if(var2 != null) {
            if(e.a(var2.getScale(), 0, 2)) {
                var1.append("s_" + var2.getScale()).append(",");
            }

            if(e.a(var2.getMaxWidth(), 1, 4096)) {
                var1.append("w_" + var2.getMaxWidth()).append(",");
            }

            if(e.a(var2.getMaxHeight(), 1, 4096)) {
                var1.append("h_" + var2.getMaxHeight()).append(",");
            }

            if(e.a(var2.getQuality(), 1, 100)) {
                var1.append("q_" + var2.getQuality()).append(",");
            }

            if(null != var2.getFormat()) {
                var1.append("f_" + var2.getFormat().name()).append(",");
            }

            if(e.a(var2.getAngle(), -360, 360)) {
                var1.append("a_" + var2.getAngle()).append(",");
            }

            if(null != var2.getDisplay()) {
                var1.append("d_" + var2.getDisplay().name()).append(",");
            }

            var1.append("l_").append(var2.isLimit()?1:0);
            var1.append("|");
            if(e.a(var2.getOffsetX(), 0, 4096)) {
                var1.append("x_").append(var2.getOffsetX()).append(",");
            }

            if(e.a(var2.getOffsetY(), 0, 4096)) {
                var1.append("y_").append(var2.getOffsetY()).append(",");
            }

            if(e.a(var2.getCropWidth(), 1, 4096)) {
                var1.append("w_").append(var2.getCropWidth()).append(",");
            }

            if(e.a(var2.getCropHeight(), 1, 4096)) {
                var1.append("h_").append(var2.getCropHeight()).append(",");
            }

            var1.append("c_").append(var2.isCrop()?1:0);
        }

        if(var3 != null) {
            if(!TextUtils.isEmpty(var1.toString())) {
                var1.append("|");
            }

            if(!TextUtils.isEmpty(var3.getObjectKey())) {
                var1.append("k_").append(var3.getObjectKey()).append(",");
            }

            if(e.a(var3.getGravity(), 1, 9)) {
                var1.append("g_").append(var3.getGravity()).append(",");
            }

            if(e.a(var3.getGravityX(), -4096, 4096)) {
                var1.append("x_").append(var3.getGravityX()).append(",");
            }

            if(e.a(var3.getGravityY(), -4096, 4096)) {
                var1.append("y_").append(var3.getGravityY()).append(",");
            }

            if(e.a(var3.getAngle(), -360, 360)) {
                var1.append("a_").append(var3.getAngle()).append(",");
            }

            if(e.a(var3.getOpacity(), 1, 100)) {
                var1.append("o_").append(var3.getOpacity()).append(",");
            }

            var1.append("wm_1");
        }

        if(var4 != null) {
            if(!TextUtils.isEmpty(var1.toString())) {
                var1.append("|");
            }

            if(!TextUtils.isEmpty(var4.getText())) {
                var1.append("t_").append(var4.getText()).append(",");
            }

            if(e.a(var4.getGravity(), 1, 9)) {
                var1.append("g_").append(var4.getGravity()).append(",");
            }

            if(e.a(var4.getGravityX(), -4096, 4096)) {
                var1.append("x_").append(var4.getGravityX()).append(",");
            }

            if(e.a(var4.getGravityY(), -4096, 4096)) {
                var1.append("y_").append(var4.getGravityY()).append(",");
            }

            if(e.a(var4.getAngle(), -360, 360)) {
                var1.append("a_").append(var4.getAngle()).append(",");
            }

            if(e.a(var4.getFontSize(), 1, 1024)) {
                var1.append("sz_").append(var4.getFontSize()).append(",");
            }

            if(!TextUtils.isEmpty(var4.getFontColor())) {
                var1.append("fc_").append(var4.getFontColor()).append(",");
            }

            if(null != var4.getFontFamily()) {
                var1.append("ff_").append(Base64.encodeToString(var4.getFontFamily().name().getBytes(), 2)).append(",");
            }

            if(null != var4.getFontStyle()) {
                var1.append("fs_").append(var4.getFontStyle().name()).append(",");
            }

            var1.append("wm_2");
        }

        return var1.toString();
    }

    public static void a(BaseRequest var0, int var1, boolean var2, int var3, String var4, OnBosListener var5, Handler var6) {
        int var7 = var0.getTag();
        BosObjectRequest var8 = (BosObjectRequest)var0;
        StringBuffer var9 = new StringBuffer();
        switch(var1) {
            case 1:
                BosPutObjectResponse var13 = new BosPutObjectResponse(var7, var3, var4);
                if(var2) {
                    var13.setObjectKey(var8.getObjectKey());
                    var13.setObjectType(var8.getObjectType());
                    if(a(var4, var9, var13)) {
                        a(1, var9.toString(), var8, var13, var5, var6);
                        return;
                    }
                }

                var6.post(new i(var5, var13));
                break;
            case 2:
                BosGetObjectResponse var12 = new BosGetObjectResponse(var7, var3, var4);
                if(var2) {
                    var12.setObjectKey(var8.getObjectKey());
                    var12.setObjectType(var8.getObjectType());
                    if(a(var4, var9, var12)) {
                        a(2, var9.toString(), var8, var12, var5, var6);
                        return;
                    }
                }

                var6.post(new j(var5, var12));
                break;
            case 3:
                BosGeneratePresignedUrlResponse var11 = new BosGeneratePresignedUrlResponse(var7, var3, var4);
                if(var2) {
                    var11.setObjectKey(var8.getObjectKey());
                    var11.setObjectType(var8.getObjectType());
                    if(a(var4, var9, var11)) {
                        var11.setStatus(0);
                        var11.setMessage("成功");
                    }
                }

                var5.onGeneratePresignedUrlCallback(var11);
        }

        InputStream var14;
        if(var0 instanceof BosPutObjectRequest && null != (var14 = ((BosPutObjectRequest)var0).getStreamData())) {
            try {
                var14.close();
                return;
            } catch (Exception var10) {
                ;
            }
        }

    }

    private static boolean a(String var0, StringBuffer var1, BosObjectResponse var2) {
        if(TextUtils.isEmpty(var0)) {
            var2.setStatus(14001);
            var2.setMessage("请求失败");
            return false;
        } else if("响应解析失败".equals(var0)) {
            var2.setStatus(14000);
            var2.setMessage("响应解析失败");
            return false;
        } else if("请求失败".equals(var0)) {
            var2.setStatus(14001);
            var2.setMessage("请求失败");
            return false;
        } else if(!a.b(var0, var2)) {
            return false;
        } else {
            try {
                JSONObject var4 = new JSONObject(var0);
                if(var4 != null && var4.has("status") && 0 == var4.getInt("status")) {
                    if(var4.has("url") && var2 instanceof BosGeneratePresignedUrlResponse) {
                        ((BosGeneratePresignedUrlResponse)var2).setUrl(var4.getString("url"));
                    }

                    if(var4.has("bucket")) {
                        var1.append(var4.getString("bucket"));
                    }

                    return true;
                } else if(var4.has("status")) {
                    var2.setStatus(var4.getInt("status"));
                    if(var4.has("message")) {
                        var2.setMessage(var4.getString("message"));
                    }

                    return false;
                } else {
                    var2.setStatus(14000);
                    var2.setMessage("响应解析失败");
                    return false;
                }
            } catch (JSONException var3) {
                var2.setStatus(14000);
                var2.setMessage("响应解析失败");
                return false;
            }
        }
    }

    private static void a(int var0, String var1, BosObjectRequest var2, BosObjectResponse var3, OnBosListener var4, Handler var5) {
        if(a == null) {
            var3.setStatus(16000);
            var3.setMessage("存储服务未初始化");
            a(var0, var3, var4, var5);
            b = false;
            if(!bc.a().c()) {
                LBSTraceClient.a.a();
            }

        } else if(!b) {
            var3.setStatus(14001);
            var3.setMessage("请求失败");
            a(var0, var3, var4, var5);
        } else {
            long var6 = g.b;

            try {
                switch(var0) {
                    case 1:
                        BosPutObjectRequest var10001 = (BosPutObjectRequest)var2;
                        BosPutObjectResponse var8 = (BosPutObjectResponse)var3;
                        BosPutObjectRequest var26 = var10001;
                        ObjectMetadata var11;
                        (var11 = new ObjectMetadata()).setContentLength(var26.getObjectSize());
                        if(BosObjectType.image == var26.getObjectType()) {
                            var11.setContentType("image/jpeg");
                        }

                        long var17 = var26.getServiceId();
                        PutObjectResponse var12 = null;
                        if(null != var26.getFile()) {
                            var12 = a.putObject(var1, "/" + String.valueOf(var17) + "/" + var26.getObjectKey(), var26.getFile(), var11);
                        } else if(null != var26.getStreamData()) {
                            var12 = a.putObject(var1, "/" + String.valueOf(var17) + "/" + var26.getObjectKey(), var26.getStreamData(), var11);
                        } else if(null != var26.getByteArray()) {
                            var12 = a.putObject(var1, "/" + String.valueOf(var17) + "/" + var26.getObjectKey(), var26.getByteArray(), var11);
                        } else if(null != var26.getStringData()) {
                            var12 = a.putObject(var1, "/" + String.valueOf(var17) + "/" + var26.getObjectKey(), var26.getStringData(), var11);
                        }

                        if(var12 != null) {
                            var8.setStatus(0);
                            var8.setMessage("成功");
                            var8.setETag(var12.getETag());
                        }

                        var5.post(new l(var4, var8));
                        return;
                    case 2:
                        a(var1, var2, (BosGetObjectResponse)var3, var4, var5);
                    default:
                }
            } catch (BceServiceException var20) {
                BceServiceException var23 = var20;
                Class var24 = h.class;
                synchronized(h.class) {
                    if(400 == var23.getStatusCode() && b && var6 == g.b) {
                        b = false;
                        if(!bc.a().c()) {
                            LBSTraceClient.a.a();
                        }
                    }
                }

                short var25 = 14001;
                String var27 = "请求失败";
                if(404 == var20.getStatusCode()) {
                    var25 = 16001;
                    var27 = "指定objectKey不存在";
                }

                var3.setStatus(var25);
                var3.setMessage(var27);
                a(var0, var3, var4, var5);
            } catch (BceClientException var21) {
                var3.setStatus(14001);
                var3.setMessage("请求失败");
                a(var0, var3, var4, var5);
            } catch (IOException var22) {
                var3.setStatus(14001);
                var3.setMessage("请求失败");
                a(var0, var3, var4, var5);
            }
        }
    }

    private static void a(int var0, BosObjectResponse var1, OnBosListener var2, Handler var3) {
        var3.post(new k(var0, var2, var1));
    }

    private static void a(String var0, BosObjectRequest var1, BosGetObjectResponse var2, OnBosListener var3, Handler var4) throws IOException {
        BosObject var8 = a.getObject(var0, "/" + String.valueOf(var1.getServiceId()) + "/" + var1.getObjectKey());
        var2.setMetaData(var8.getObjectMetadata());
        BosObjectInputStream var9 = var8.getObjectContent();
        ByteArrayOutputStream var10 = new ByteArrayOutputStream();
        byte[] var5 = new byte[4096];
        boolean var6 = false;
        if(var9 != null) {
            while(true) {
                int var11;
                if((var11 = var9.read(var5, 0, 4096)) <= 0) {
                    var2.setStatus(0);
                    var2.setMessage("成功");
                    var2.setObjectContent(var10);
                    break;
                }

                var10.write(var5, 0, var11);
            }
        }

        var4.post(new m(var3, var2));

        try {
            if(var10 != null) {
                var10.close();
            }

            if(var9 != null) {
                var9.close();
            }

        } catch (IOException var7) {
            ;
        }
    }
}
